package com.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.entity.Consumer;
import com.google.gson.Gson;
import com.servers.ConsumerServers;

@Controller
public class ConsumerController {
	@Autowired
	private ConsumerServers consumerServers;
	@RequestMapping("/zhuxiao")
	public String zhuxiao(HttpSession session) {
		session.removeAttribute("CURR_COM");
		
		return "../../index";
	}
	
    @ResponseBody
	@RequestMapping("/login")
	public ModelAndView userLogin(String uname , String pwd , HttpSession session) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("admin");
        List<Consumer> clist = consumerServers.userLogin(uname, pwd);
        mv.addObject("clist",clist);
        for (Consumer consumer : clist) {
			
        	System.err.println("hello:"+consumer);
		}
       //session.setAttribute("CURR_CSM", );
        
		return mv;
	}
	
	@RequestMapping("/addCnsumer")
	public ModelAndView addConsumer(String uname, String pwd, String tel, String address) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("consumerlogin");
        int num = consumerServers.addConsumer(uname, pwd, tel, address);
        mv.addObject("num", num);
		return mv;
	}
	@ResponseBody
	@RequestMapping("/findAllConsumer")
	public String findAllConsumer() {
		System.out.println("============================");
        ModelAndView mv = new ModelAndView();
        List<Consumer> clist = consumerServers.findAllConsumer();
        List<Map<Object,Object>> list = new ArrayList<>();
        
        Map<Object,Object> map2 = new HashMap<>();
        for (Consumer consumer : clist) {
        	Map<Object,Object> map = new HashMap<>();
			map.put("id", consumer.getCid());
			map.put("name", consumer.getCname());
			map.put("pwd", consumer.getCpwd());
			map.put("tel", consumer.getCtel());
			map.put("address", consumer.getCaddress());
			//System.out.println(map);
			list.add(map);
			//System.out.println("============"+list);
		}
        map2.put("code", 0);
        map2.put("msg", "");
        map2.put("count", 5);
        map2.put("data", list);
        
       // System.out.println(map2);
        Gson gson = new Gson();
        //System.out.println(gson.toJson(map));
        
		return gson.toJson(map2);
	}
	@ResponseBody
	@RequestMapping("/deleteConsumer")
	public String  deleteConsumer(int cid) {
     System.out.println("--------------进入控制cid!!!!!!!!!!!!类----------------"+cid);   
        int num = consumerServers.deleteConsumer(cid);
        
		return "";
	}
	@ResponseBody
	@RequestMapping("/findConsumerByCname")
	public String findConsumerByCname(String cname) {
		//System.out.println("=============="+cname);
        ModelAndView mv = new ModelAndView();
        /*mv.setViewName("rol.jsp");*/
        List<Consumer> clist = consumerServers.findConsumerByCname(cname);
        
        List<Map<Object,Object>> list = new ArrayList<>();
        
        Map<Object,Object> map2 = new HashMap<>();
        for (Consumer consumer : clist) {
        	Map<Object,Object> map = new HashMap<>();
			map.put("id", consumer.getCid());
			map.put("name", consumer.getCname());
			map.put("pwd", consumer.getCpwd());
			map.put("tel", consumer.getCtel());
			map.put("address", consumer.getCaddress());
			//System.out.println(map);
			list.add(map);
		//	System.out.println("============"+list);
		}
        map2.put("code", 0);
        map2.put("msg", "");
        map2.put("count", 5);
        map2.put("data", list);
        
       // System.out.println(map2);
        Gson gson = new Gson();
        
		return gson.toJson(map2);
	}

	public ConsumerServers getConsumerServers() {
		return consumerServers;
	}

	public void setConsumerServers(ConsumerServers consumerServers) {
		this.consumerServers = consumerServers;
	}

}
